import Mock from 'mockjs'

const LineData = Mock.mock({
  'data|7': [{
    'date|+1': 20221011,
    apple: '@integer(1000, 8000)',
    huawei: '@integer(1000, 8000)',
    xiaomi: '@integer(1000, 8000)'
  }]
})

const PieData = Mock.mock({
  'data|3': [{
    'name|+1': ['苹果', '华为', '小米'],
    value: '@integer(2000, 4000)'
  }]
})

const BarData = Mock.mock({
  'data|7': [{
    'date|+1': ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    new: '@integer(100, 200)',
    active: '@integer(100, 500)'
  }]
})

const TableData = Mock.mock({
  'data|3': [{
    'name|+1': ['苹果', '华为', '小米'],
    todayBuy: '@integer(10, 100)',
    monthBuy: '@integer(500, 2000)',
    totalBuy: '@integer(5000, 9000)'
  }]
})

Mock.Random.extend({
  phone() {
    const phonePrefixs = ['135', '189', '136'] // 自己随便写前缀
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) // 使用pick随机返回前缀
  }
})

const UserList = Mock.mock({
  'data|50': [{ // 生成50条数据
    'id|+1': 1, // id会自增
    username: '@cname', // 随机生成姓名 --中文名
    sex: '@integer(0, 1)', // 性别一般是用数字表示 1男生 0女生
    phone: Mock.Random.phone(), // 拓展mockjs 生成随机的手机号码
    email: '@email', // 随机生成邮箱地址
    'age|18-60': 18, // 随机生成年龄
    address: '@county(true)', // 随机生成地址
    status: '@integer(0, 1)', // 随机生成账户状态
    thumb: '@image( 25x25, #FF6600, #fff, png, 头像 )', // 随机生成头像
    date: '@date(yyyy-MM-dd)',
    createTime: '@datetime(yyyy-MM-dd HH:mm:ss)', // 随机生成创建时间
    role: '@pick(["管理员", "普通用户", "开发用户", "测试用户"])', // 随机生成角色
    ip: '@ip'// 模拟ip地址
  }]
})

export default [
  {
    url: '/getLineData',
    method: 'get',
    response: () => {
      return { code: 200, message: "ok", data: LineData.data }
    }
  },
  {
    url: '/getPieData',
    method: 'get',
    response: () => {
      return { code: 200, message: "ok", data: PieData.data }
    }
  },
  {
    url: '/getBarData',
    method: 'get',
    response: () => {
      return { code: 200, message: "ok", data: BarData.data }
    }
  },
  {
    url: '/getTableData',
    method: 'get',
    response: () => {
      return { code: 200, message: "ok", data: TableData.data }
    }
  },
  {
    url: '/getUserList',
    method: 'post',
    response: ({ body }) => {
      const { page, limit, name } = body
      // console.log(body) // 会在终端打印，不在浏览器打印
      let filterList = UserList.data
      if (name && name.trim().length !== 0) {
        filterList = UserList.data.filter((item) => item.name.includes(name))
      }
      const pageList = filterList.slice(limit * (page - 1), limit * page)
      return { code: 200, message: "ok", data: { rows: pageList, total: filterList.length } }
    }
  },
  {
    url: '/deleteUser',
    method: 'post',
    response: ({ body }) => {
      const { id } = body
      for (let i = 0; i < id.length; i++) {
        const index = UserList.data.findIndex((item) => item.id === id[i])
        UserList.data.splice(index, 1)
      }
      return { code: 200, message: "ok", data: '' }
    }
  },
  {
    url: '/updateUser',
    method: 'post',
    response: ({ body }) => {
      const target = UserList.data.find((item) => item.id === body.id)
      for (const key in body) {
        target[key] = body[key]
      }
      return { code: 200, message: "ok", data: target }
    }
  },
  {
    url: '/addUser',
    method: 'post',
    response: ({ body }) => {
      UserList.data.unshift(body)
      return { code: 200, message: "ok", data: '' }
    }
  },
  {
    url: '/getUserById',
    method: 'get',
    response: (data) => {
      const id = data.query.id
      return { code: 200, message: "ok", data: UserList.data.find((item) => item.id === Number(id)) }
    }
  }
]